

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;



import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

/**
 * 对OSS服务器进行上传删除等的处理
 * 
 * @ClassName: OSSManageUtil
 *
 */
public class OSSTest1111 {
	/**
	 * 上传OSS服务器文件 @Title: uploadFile @Description: @param file
	 * 需要上传的图片或者其他 @return 返回可直接访问的阿里云图片全地址 @throws
	 */
	public static String uploadFile(InputStream content, String fileName, 
			                       String youKey,String alyEndPoint,
			                       String alyKey,String alySecretKey,
			                       String alyBucketName,String alyForwordEndPoint) throws Exception {
		/*if (StringUtils.isEmpty(alyEndPoint) 
			|| StringUtils.isEmpty(alyKey) 
			|| StringUtils.isEmpty(alySecretKey)
			|| StringUtils.isEmpty(alyBucketName) 
			|| StringUtils.isEmpty(youKey)) {
			return null;
		}
		if(StringUtils.isEmpty(alyForwordEndPoint)){
			alyForwordEndPoint=alyEndPoint;
		}*/
		
		OSSClient ossClient = new OSSClient(alyEndPoint, alyKey, alySecretKey);
		String remoteFilePath = youKey.replaceAll("\\\\", "/") + "/";
		// 创建上传Object的Metadata
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(content.available());
		objectMetadata.setCacheControl("no-cache");
		objectMetadata.setHeader("Pragma", "no-cache");
		if ("".equals(fileName) && fileName.lastIndexOf(".") >= 0) {
			objectMetadata.setContentType(contentType(fileName.substring(fileName.lastIndexOf(".") + 1)));
		} else {
			objectMetadata.setContentType(contentType("jpeg"));
		}
		objectMetadata.setContentDisposition("inline;filename=" + fileName);
		// 上传文件
		PutObjectResult putObjectResult = ossClient.putObject(alyBucketName, remoteFilePath + fileName, content, objectMetadata);
		if (putObjectResult == null 
			|| putObjectResult.getETag() == null) {
			throw new Exception("文件上传到阿里云失败");
		}
		return alyForwordEndPoint + "/" + alyBucketName + "/" + youKey + "/" + fileName;
	}

	
	

	/**
	 * Description: 判断OSS服务文件上传时文件的contentType @Version1.0
	 * 
	 * @param FilenameExtension
	 *            文件后缀
	 * @return String
	 */
	public static String contentType(String FilenameExtension) {
		FilenameExtension = FilenameExtension.toUpperCase();
		if (FilenameExtension.equals("BMP")) {
			return "image/bmp";
		}
		if (FilenameExtension.equals("GIF")) {
			return "image/gif";
		}
		if (FilenameExtension.equals("JPEG") || FilenameExtension.equals("JPG") || FilenameExtension.equals("PNG")) {
			return "image/jpeg";
		}
		if (FilenameExtension.equals("HTML")) {
			return "text/html";
		}
		if (FilenameExtension.equals("TXT")) {
			return "text/plain";
		}
		if (FilenameExtension.equals("VSD")) {
			return "application/vnd.visio";
		}
		if (FilenameExtension.equals("PPTX") || FilenameExtension.equals("PPT")) {
			return "application/vnd.ms-powerpoint";
		}
		if (FilenameExtension.equals("DOCX") || FilenameExtension.equals("DOC")) {
			return "application/msword";
		}
		if (FilenameExtension.equals("XML")) {
			return "text/xml";
		}
		if (FilenameExtension.equals("ZIP")) {
			return "application/zip";
		}
		if (FilenameExtension.equals("APK")) {
			return "application/vnd.android.package-archive";
		}
		if (FilenameExtension.equals("IPA")) {
			return "application/vnd.iphone";
		}
		return "text/html";
	}
	public static void main(String[]st)throws Exception{
		InputStream fileContent= fileContent=new FileInputStream(new File("F:/tu//test1.jpg"));
		
		String s=uploadFile(fileContent, "test1.jpg", "app/head", "http://oss-cn-shenzhen.aliyuncs.com", "JXq8tWZB52dgLHj5", "7iudzkRWVRO21JJr00ZjeyY4LDCTr6","lldd-test", "http://d.redpackets.cn");
		System.out.println(s);
	    
	    
	}
}
